﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using WoSoft.PACS.Win.MyToolBars;
using WoSoft.PACS.Data.Patient;

namespace WoSoft.PACS.Win.Forms
{
    /// <summary>
    /// 病人列表
    /// </summary>
    public partial class FrmPatientList : FrmBase
    {
        PatientData patientdata = new PatientData();

        public FrmPatientList()
        {
            InitializeComponent();
        }

        public FrmPatientList(RibbonBar ribar)
            : base(ribar)
        {
            InitializeComponent();
        }

        public override List<MyToolBars.OperationModel> Operations()
        {
            List<OperationModel> functionInfos = new List<OperationModel>();

            //创建病人
            var createButton = new OperationModel()
            {
                ID = Guid.NewGuid().ToString(),
                Name = "病人建档",
                Image = global::WoSoft.PACS.Win.Properties.Resources.add2,
                ClickEvent = new EventHandler(CreatePatientClick),
            };
            functionInfos.Add(createButton);

            //修改病人
            var editButton = new OperationModel()
            {
                ID = Guid.NewGuid().ToString(),
                Name = "编辑档案",
                Image = global::WoSoft.PACS.Win.Properties.Resources.document_edit,
                ClickEvent = new EventHandler(EditPatientClick),
            };
            functionInfos.Add(editButton);

            //删除病人
            var deleteButton = new OperationModel()
            {
                ID = Guid.NewGuid().ToString(),
                Name = "删除档案",
                Image = global::WoSoft.PACS.Win.Properties.Resources.document_delete,
                ClickEvent = new EventHandler(DeletePatientClick),
            };
            functionInfos.Add(deleteButton);

            //刷新
            var refreshButton = new OperationModel()
            {
                ID = Guid.NewGuid().ToString(),
                Name = "刷新数据",
                Image = global::WoSoft.PACS.Win.Properties.Resources.refresh,
                ClickEvent = new EventHandler(RefreshClick),
            };
            functionInfos.Add(refreshButton);
            return functionInfos;
        }

        //病人建档
        private void CreatePatientClick(object sender, EventArgs e)
        {
            SubForms.FrmPatientInfo patientInfo = new SubForms.FrmPatientInfo();
            patientInfo.ShowDialog(this);
            LoadData();
        }

        //修改病人
        private void EditPatientClick(object sender, EventArgs e)
        {
            if (this.superGridControl1.GetSelectedRows().Count > 0)
            {
                DevComponents.DotNetBar.SuperGrid.GridRow gridRow = this.superGridControl1.GetSelectedRows()[0] as DevComponents.DotNetBar.SuperGrid.GridRow;
                SubForms.FrmPatientInfo patientInfo = new SubForms.FrmPatientInfo();
                patientInfo.ID = gridRow.Cells["Column0"].Value.ToString();
                patientInfo.ShowDialog(this);

                //刷新用户
                LoadData();
            }
        }

        //删除病人
        private void DeletePatientClick(object sender, EventArgs e)
        {
            if (this.superGridControl1.GetSelectedRows().Count > 0)
            {
                if (MessageBoxEx.Show("您确认要删除选择的病人档案吗?", "删除", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != System.Windows.Forms.DialogResult.OK)
                {
                    return;
                }
                DevComponents.DotNetBar.SuperGrid.GridRow gridRow = this.superGridControl1.GetSelectedRows()[0] as DevComponents.DotNetBar.SuperGrid.GridRow;
                patientdata.Delete(gridRow.Cells["Column0"].Value.ToString());
                //刷新用户
                LoadData();
            }
            else
            {
                MessageBoxEx.Show("请选择一个需要删除的病人档案");
            }
        }

        //刷新
        private void RefreshClick(object sender, EventArgs e)
        {
            LoadData();
        }

        //查询病人
        private void btnSearch_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        private void LoadData()
        {
            var dt = patientdata.GetAllData();
            this.superGridControl1.PrimaryGrid.DataSource = dt;
        }
    }
}
